#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e5+1000;
typedef long long LL;
typedef pair<LL,LL>P;
LL a[maxn],sum[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i],sum[i]=sum[i-1]+a[i];
  if(sum[n]%3!=0){
    cout<<"0"<<endl;
  }
  else{
    LL ans=0;LL num=0;
    for(LL i=1;i<n;i++){
        if(sum[i]*3==2*sum[n]){
            ans+=num;
        }
        if(sum[i]*3==sum[n]){
            num++;
        }
    }
    cout<<ans<<endl;
  }
return 0;
}
